
$(function() {
    validateRule();
    $('.imgcode').click(function() {
		var url = ctx + "captcha/captchaImage?type=" + captchaType + "&s=" + Math.random();
		$(".imgcode").attr("src", url);
	}
    );

    $("#xueyuan").change(function () {
        //alert("hello");
        //请求参数对象
        var ctx1 = "http://" + window.location.host;
        var xueyuanid = $.common.trim($("#xueyuan option:selected").val());
        //alert(ctx1+"/system/class/listbyid/"+xueyuanid);
        //执行Ajax请求
        $.ajax({
            type: "GET",
            url: ctx1+"/system/class/listbyid/"+xueyuanid,
            async: true,
            dataType: "json",
            contentType: "application/json; charset=UTF-8",
            //data: JSON.stringify(xueyuanid),
            success: function (data) {
                //绑定数据
                //data = JSON.parse(result);
                console.log(data);
                var nameOpt = "<option value='' selected='selected'>--请选择--</option>";
                for(var i=0;i<data.length;i++) {

                    nameOpt+="<option value='"+data[i].id+"' >"+data[i].className+"</option>"
                }
                $('#banji').html(nameOpt);

            }
        });
    });
});

$.validator.setDefaults({
    submitHandler: function() {
    	register();
    }
});
function phoneFun(phones){
    var myreg = /^[1][3,4,5,7,8,9][0-9]{9}$/;
    if (!myreg.test(phones)) {
        $.modal.msg("手机号格式错误");
        return false;
    }
    return true;
}
function register() {
	$.modal.loading($("#btnSubmit").data("loading"));
	var loginName = $.common.trim($("input[name='loginName']").val());
    var password = $.common.trim($("input[name='password']").val());
	var userName = $.common.trim($("input[name='userName']").val());
	var xueyuan = $.common.trim($("#xueyuan option:selected").text());
	var banji = $.common.trim($("#banji option:selected").text());
	var phonenumber = $.common.trim($("input[name='phonenumber']").val());
    phoneFun(phonenumber);
    if (false == phoneFun(phonenumber)){
        $("input[name='phonenumber']").clear();
        return false;
    }
	var loginName = $.common.trim($("input[name='loginName']").val());
    var validateCode = $("input[name='validateCode']").val();
    $.ajax({
        type: "post",
        url: ctx + "register",
        data: {
            "loginName": loginName,
            "userName": userName,
            "password": password,
            "xueyuan": xueyuan,
            "banji": banji,
            "phonenumber": phonenumber,
            "validateCode": validateCode
        },
        success: function(r) {
            if (r.code == 0) {
            	layer.alert("<font color='red'>恭喜你，您的账号 " + loginName + " 注册成功！</font>", {
        	        icon: 1,
        	        title: "系统提示"
        	    },
        	    function(index) {
        	        //关闭弹窗
        	        layer.close(index);
        	        location.href = ctx + 'login';
        	    });
            } else {
            	$.modal.closeLoading();
            	$('.imgcode').click();
            	$(".code").val("");
            	$.modal.msg(r.msg);
            }
        }
    });
}

function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
    $("#registerForm").validate({
        rules: {
            username: {
                required: true,
                minlength: 2
            },
            password: {
                required: true,
                minlength: 5
            },
            confirmPassword: {
                required: true,
                equalTo: "[name='password']"
            }
        },
        messages: {
            username: {
                required: icon + "请输入您的用户名",
                minlength: icon + "用户名不能小于2个字符"
            },
            password: {
            	required: icon + "请输入您的密码",
                minlength: icon + "密码不能小于5个字符",
            },
            confirmPassword: {
                required: icon + "请再次输入您的密码",
                equalTo: icon + "两次密码输入不一致"
            }
        }
    })
}
